package com.david.func_test.util

import android.os.Handler
import android.os.Looper
import android.util.Log
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.firstOrNull
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.flowOn
import java.util.concurrent.Flow
import kotlin.coroutines.resume
import kotlin.coroutines.suspendCoroutine
import io.reactivex.functions.Consumer
import kotlinx.coroutines.flow.collect

object TestUtil {
    fun test(): kotlinx.coroutines.flow.Flow<Int> {
        return flow {
            Log.e("main", "TestUtil test...")
//            var result = flow<Int> {

                val isInit = initBleIfIndeed()


//                Thread.sleep(20000)

                Log.e("main", "TestUtil flow test...")

//                emit(0)
//            }.flowOn(Dispatchers.IO).collect {
//                Log.e("main", "TestUtil flow test collect...")
//            }

            Log.e("main", "TestUtil flow test end...")

            emit(0)
        }
    }
    private suspend fun initBleIfIndeed() {
        val bleInitCode = initBleSdk()
    }
    private suspend fun initBleSdk(): Int = suspendCoroutine { continuation ->
        initBleSdk { isInit -> continuation.resume(isInit) }
    }

    private fun initBleSdk(consumer: Consumer<Int>) {
        Handler(Looper.getMainLooper()).postDelayed({
            consumer.accept(0)
        }, 20000)
    }
}